import 'package:citiesocial/_include.dart';

///
///单个商品的组件，长方行的，默认只需要传Goods这个bean类，同时可以传图片的宽、高（默认200）、整体的宽、对外的margin
///
class LongGoodsItemIndex extends BaseStatefulWidget {
  final ActicityEntity activityEntity;
  final double height;
  final bool alarmable, timedown;
  final GestureTapCallback onTap;

  LongGoodsItemIndex(this.activityEntity,
      {this.alarmable: false,
      this.timedown: false,
      this.height: 200.0,
      this.onTap});

  @override
  State<StatefulWidget> createState() => _LongGoodsItemState();
}

class _LongGoodsItemState extends BaseState<LongGoodsItemIndex> {
  _LongGoodsItemState();

  ///
  ///设置商品提醒
  ///
  void _goodsAlarm() async {
    final alertNew = widget.activityEntity.is_alert == '1' ? '0' : '1';
    final equid = await getDeviceId();
    final bool result = await HttpManager.remind(
        alertNew, equid, widget.activityEntity.activity_id);
    if (result) {
      setState(() {
        widget.activityEntity.is_alert = alertNew;
      });
    }
  }

  void _goActicityDetail() {
    Navigator.push(
      context,
      MaterialPageRoute(
          builder: (BuildContext context) => ActivityDetail(
                activityEntity: widget.activityEntity,
              )),
    );
  }

  @override
  Widget build(BuildContext context) => Container(
        child: GestureDetector(
          child: Stack(
            fit: StackFit.expand,
            children: <Widget>[
              Image(
                image: CachedNetworkImageProvider(
                  widget.activityEntity.img,
                ),
                height: widget.height,
                fit: BoxFit.cover,
              ),
              Align(
                alignment: Alignment.topRight,
                child: Offstage(
                  offstage: !widget.alarmable,
                  child: GestureDetector(
                    child: Container(
                      child: Image.asset(
                        widget.activityEntity.is_alert == '1'
                            ? 'imgs/js_cells_img_btn_notifycircle_selected.png'
                            : 'imgs/js_cells_img_btn_notifycircle_n.png',
                        width: 60.0,
                        height: 60.0,
                      ),
                      margin: EdgeInsets.all(10.0),
                    ),
                    onTap: _goodsAlarm,
                  ),
                ),
              ),
              Align(
                child: Wrap(
                  children: <Widget>[
                    Container(
                      child: Text(
                        widget.activityEntity.activity_name ?? '',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 15.0,
                        ),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                      width: double.infinity,
                      margin: EdgeInsets.symmetric(horizontal: 10.0),
                    ),
                    Container(
                      child: Row(
                        // mainAxisAlignment: MainAxisAlignment.spaceAround,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          Expanded(
                            child: Text(
                              widget.activityEntity.activity_name ?? '',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 17.0,
                              ),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                          Image.asset(
                            'imgs/js_cells_img_more_w.png',
                            width: 20.0,
                            height: 20.0,
                          )
                        ],
                      ),
                      width: double.infinity,
                      margin:
                          EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
                    ),
                    Offstage(
                      offstage: !widget.timedown,
                      child: Column(
                        children: <Widget>[
                          Container(
                            decoration: BoxDecoration(
                                shape: BoxShape.rectangle, color: Colors.white),
                            height: 1.0,
                            margin: EdgeInsets.symmetric(horizontal: 10.0),
                          ),
                          Container(
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  flex: 8,
                                  child: Align(
                                    child: Text(
                                      widget.activityEntity.finish_time ?? '',
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 17.0,
                                      ),
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                    alignment: Alignment.centerLeft,
                                  ),
                                ),
                                Expanded(
                                  flex: 10,
                                  child: Align(
                                    child: Text(
                                      'adsfadsfa',
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 17.0,
                                      ),
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                    alignment: Alignment.centerRight,
                                  ),
                                ),
                              ],
                            ),
                            margin: EdgeInsets.all(10.0),
                          )
                        ],
                      ),
                    ),
                  ],
                ),
                alignment: Alignment.bottomCenter,
              ),
            ],
          ),
          onTap: widget.onTap == null ? _goActicityDetail : widget.onTap,
        ),
        height: widget.height,
      );
}
